Passed
Push — master ( 83670a...7f4634 )
by Rafael S.
02:44
created

RIFFFile.getSubChunkIndex_   A

Complexity

Conditions 3

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
dl 0
loc 22
rs 9.6
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2017-2019 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview The RIFFFile class.
27
 * @see https://github.com/rochars/wavefile
28
 */
29
30
/** @module wavefile */
31
32
import {unpackString, unpack} from 'byte-data';
33
34
/**
35
 * A class to perform low-level reading of RIFF/RIFX files.
36
 */
37
export default class RIFFFile {
38
39
  constructor() {
40
    
41
    /** @type {number} */
42
    this.head_ = 0;
43
    /** @type {!Object} */
44
    this.uInt32_ = {bits: 32, be: false};
45
    /** @type {!Object} */
46
    this.uInt16_ = {bits: 16, be: false};
47
    /**
48
     * The container identifier.
49
     * 'RIFF', 'RIFX' and 'RF64' are supported.
50
     * @type {string}
51
     */
52
    this.container = '';
53
    /**
54
     * @type {number}
55
     */
56
    this.chunkSize = 0;
57
    /**
58
     * The format.
59
     * @type {string}
60
     */
61
    this.format = '';
62
    /**
63
     * A object defining the start and end of all chunks in a wav buffer.
64
     * @type {!Object}
65
     */
66
    this.signature = {};
67
  }
68
69
  /**
70
   * Read the signature of the chunks in a RIFF/RIFX file.
71
   * @param {!Uint8Array} buffer The file bytes.
72
   * @protected
73
   */
74
  setSignature(buffer) {
75
      this.head_ = 0;
76
      /** @type {string} */
77
      let chunkId = this.getChunkId_(buffer, 0);
78
      this.uInt32_.be = chunkId == 'RIFX';
79
      /** @type {string} */
80
      let format = unpackString(buffer, 8, 12);
81
      this.head_ += 4;
82
      this.signature = {
83
          chunkId: chunkId,
84
          chunkSize: this.getChunkSize_(buffer, 0),
85
          format: format,
86
          subChunks: this.getSubChunksIndex_(buffer)
87
      };
88
  }
89
90
  /**
91
    * Find a chunk by its fourCC_ in a array of RIFF chunks.
92
    * @param {string} chunkId The chunk fourCC_.
93
    * @param {boolean} multiple True if there may be multiple chunks
94
    *    with the same chunkId.
95
    * @return {Object}
96
    * @protected
97
    */
98
  findChunk(chunkId, multiple=false) {
99
    /** @type {!Array<!Object>} */
100
    let chunks = this.signature.subChunks;
101
    /** @type {!Array<!Object>} */
102
    let chunk = [];
103
    for (let i=0; i<chunks.length; i++) {
104
      if (chunks[i].chunkId == chunkId) {
105
        if (multiple) {
106
          chunk.push(chunks[i]);
107
        } else {
108
          return chunks[i];
109
        }
110
      }
111
    }
112
    if (chunkId == 'LIST') {
113
      return chunk.length ? chunk : null;
114
    }
115
    return null;
116
  }
117
118
  /**
119
   * Read the main chunk of a RIFF file.
120
   * @param {!Uint8Array} bytes A RIFF file buffer.
121
   * @throws {Error} If container is not RIFF, RIFX or RF64.
122
   * @protected
123
   */
124
  readRIFFChunk(bytes) {
125
    this.head_ = 0;
126
    this.container = this.readString(bytes, 4);
127
    if (['RIFF', 'RIFX', 'RF64'].indexOf(this.container) === -1) {
128
      throw Error('Not a supported format.');
129
    }
130
    this.uInt16_.be = this.container === 'RIFX';
131
    this.uInt32_.be = this.uInt16_.be;
132
    this.chunkSize = this.readNumber(bytes, this.uInt32_);
133
    this.format = this.readString(bytes, 4);
134
  }
135
136
  /**
137
   * Read bytes as a string from a RIFF chunk.
138
   * @param {!Uint8Array} bytes The bytes.
139
   * @param {number} maxSize the max size of the string.
140
   * @return {string} The string.
141
   * @protected
142
   */
143
  readString(bytes, maxSize) {
144
    /** @type {string} */
145
    let str = '';
146
    str = unpackString(bytes, this.head_, this.head_ + maxSize);
147
    this.head_ += maxSize;
148
    return str;
149
  }
150
151
  /**
152
   * Read a number from a chunk.
153
   * @param {!Uint8Array} bytes The chunk bytes.
154
   * @param {!Object} bdType The type definition.
155
   * @return {number} The number.
156
   * @protected
157
   */
158
  readNumber(bytes, bdType) {
159
    /** @type {number} */
160
    let size = bdType.bits / 8;
161
    /** @type {number} */
162
    let value = unpack(bytes, bdType, this.head_);
163
    this.head_ += size;
164
    return value;
165
  }
166
167
  /**
168
   * Return the sub chunks of a RIFF file.
169
   * @param {!Uint8Array} buffer the RIFF file bytes.
170
   * @return {!Array<Object>} The subchunks of a RIFF/RIFX or LIST chunk.
171
   * @private
172
   */
173
  getSubChunksIndex_(buffer) {
174
      /** @type {!Array<!Object>} */
175
      let chunks = [];
176
      /** @type {number} */
177
      let i = this.head_;
178
      while(i <= buffer.length - 8) {
179
          chunks.push(this.getSubChunkIndex_(buffer, i));
180
          i += 8 + chunks[chunks.length - 1].chunkSize;
181
          i = i % 2 ? i + 1 : i;
182
      }
183
      return chunks;
184
  }
185
186
  /**
187
   * Return a sub chunk from a RIFF file.
188
   * @param {!Uint8Array} buffer the RIFF file bytes.
189
   * @param {number} index The start index of the chunk.
190
   * @return {!Object} A subchunk of a RIFF/RIFX or LIST chunk.
191
   * @private
192
   */
193
  getSubChunkIndex_(buffer, index) {
194
      /** @type {!Object} */
195
      let chunk = {
196
          chunkId: this.getChunkId_(buffer, index),
197
          chunkSize: this.getChunkSize_(buffer, index),
198
      };
199
      if (chunk.chunkId == 'LIST') {
200
          chunk.format = unpackString(buffer, index + 8, index + 12);
201
          this.head_ += 4;
202
          chunk.subChunks = this.getSubChunksIndex_(buffer);
203
      } else {
204
          /** @type {number} */
205
          let realChunkSize = chunk.chunkSize % 2 ?
206
              chunk.chunkSize + 1 : chunk.chunkSize;
207
          this.head_ = index + 8 + realChunkSize;
208
          chunk.chunkData = {
209
              start: index + 8,
210
              end: this.head_
211
          };
212
      }
213
      return chunk;
214
  }
215
216
  /**
217
   * Return the fourCC_ of a chunk.
218
   * @param {!Uint8Array} buffer the RIFF file bytes.
219
   * @param {number} index The start index of the chunk.
220
   * @return {string} The id of the chunk.
221
   * @private
222
   */
223
  getChunkId_(buffer, index) {
224
      this.head_ += 4;
225
      return unpackString(buffer, index, index + 4);
226
  }
227
228
  /**
229
   * Return the size of a chunk.
230
   * @param {!Uint8Array} buffer the RIFF file bytes.
231
   * @param {number} index The start index of the chunk.
232
   * @return {number} The size of the chunk without the id and size fields.
233
   * @private
234
   */
235
  getChunkSize_(buffer, index) {
236
      this.head_ += 4;
237
      return unpack(buffer, this.uInt32_, index + 4);
238
  }
239
}
240